package com.example.servicetest.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.example.servicetest.R

import kotlin.math.roundToInt

/**
 * 自定义的裁剪区域框
 * 参考https://blog.csdn.net/WPR13005655989/article/details/127093551
 */
class FaceImageView:View {

    private val mPorterDuffXfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT)

    private val mPaint = Paint()

    private val mPaintX = Paint()

    private val mOutlineBitmapSrcRect:Rect = Rect()
    private val mOutlineBitmapDstRect:Rect = Rect()

    private val mBgBitmapSrcRect:Rect = Rect()

    /**
     * 背景绘制区域,同时也是裁剪区域
     */
    private val mBgBitmapDstRect:Rect = Rect()

    /**
     * 缩放后目标图标区域
     */
    private val mScaleBitmapDstRect:Rect = Rect()

    /**
     * 未缩放的目标图标区域
     */
    private val mScaleBitmapSrcRect:Rect = Rect()

    /**
     * 目标图片
     */
    private var mScaleBitmap: Bitmap? = null

    /**
     * 镂空的图层
     */
    private var mBgBitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.preview)

    /**
     * 轮廓
     */
    private var mOutlineBitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.shape_1)


    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        setLayerType(LAYER_TYPE_SOFTWARE,null)
        mPaint.run {
            isAntiAlias = true
        }
        mScaleBitmap = BitmapFactory.decodeResource(resources, R.drawable.photo)
        mPaintX.xfermode = mPorterDuffXfermode
    }

    fun setScaleBitmap(bitmap: Bitmap){
        this.mScaleBitmap = bitmap
        invalidate()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val withSize = MeasureSpec.getSize(widthMeasureSpec)
        val ratio = mOutlineBitmap.width/mOutlineBitmap.height.toFloat()
        val newHeight = (withSize/ratio).toInt()
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(newHeight,MeasureSpec.EXACTLY))
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //裁剪区域取宽度的0.8,也就是人脸图片的宽度,计算比例获取人脸的绘制区域
        val newBgBitmapWidth = width-width*0.2
        val ratio = mBgBitmap.width/mBgBitmap.height.toFloat()
        val newBgBitmapHeight = newBgBitmapWidth/ratio
        mBgBitmapDstRect.run {
            left = ((width - newBgBitmapWidth)/2).roundToInt()
            top = ((height - newBgBitmapHeight)/2).roundToInt()
            right = (left+newBgBitmapWidth).roundToInt()
            bottom = (top + newBgBitmapHeight).roundToInt()
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //绘制目标图片
        mScaleBitmap?.run {
            mScaleBitmapSrcRect.top = 0
            mScaleBitmapSrcRect.left = 0
            mScaleBitmapSrcRect.right = mScaleBitmap!!.width
            mScaleBitmapSrcRect.bottom = mScaleBitmap!!.height
            canvas.drawBitmap(mScaleBitmap!!,mScaleBitmapSrcRect,mBgBitmapDstRect, null)
        }
        canvas.save()
        val id = canvas.saveLayer(0f,0f, width.toFloat(), height.toFloat(), null)
        //绘制背景蒙版
        mPaint.color = Color.parseColor("#0F1434")
        mBgBitmapSrcRect.run {
            top = 0
            left = 0
            right = width
            bottom = height
        }
        canvas.drawRect(mBgBitmapSrcRect,mPaint)
        //绘制人脸
        mBgBitmapSrcRect.run {
            left = 0
            top = 0
            right = mBgBitmap.width
            bottom = mBgBitmap.height
        }
        //居中绘制
        canvas.drawBitmap(mBgBitmap,mBgBitmapSrcRect,mBgBitmapDstRect,mPaintX)
        canvas.restoreToCount(id)

        //绘制外轮廓
        mOutlineBitmapSrcRect.run {
            left = 0
            top = 0
            right = mOutlineBitmap.width
            bottom = mOutlineBitmap.height
        }
        mOutlineBitmapDstRect.run {
            left = 0
            top = 0
            right = width
            bottom = height
        }
        canvas.drawBitmap(
            mOutlineBitmap,
            mOutlineBitmapSrcRect,
            mOutlineBitmapDstRect,
            null)
    }

}
